<template>
  <div class="login-wrap">
    <el-form
      :label-position="labelPosition"
      label-width="auto"
      :model="formLabelAlign"
      style="max-width: 300px;"
      class="login-container"
      :rules="rules"
      ref="ruleFormRef"
      @submit.prevent="login"
    >
      <el-form-item prop="username">
        <el-input v-model="formLabelAlign.username" placeholder="请输入账号" clearable style="height: 45px; margin-bottom: 7px;" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="formLabelAlign.password" type="password" placeholder="请输入密码" show-password clearable style="height: 45px;" />
      </el-form-item>

      <div class="my-4 ml-4">
        <el-radio-group v-model="remember">
          <el-radio :value="true" size="small" style="margin-bottom: 3px;">记住密码</el-radio>
        </el-radio-group>
      </div>

      <el-form-item class="btn-box">
        <el-button type="success" style="width: 100%; background-color: #07a2ec; height: 45px;" native-type="submit">登录</el-button>
      </el-form-item>
      <el-form-item class="btn-box">
        <el-button type="primary" style="width: 100%; background-color: sienna; height: 45px;" @click="goToRegister">注册</el-button>
      </el-form-item>
      <el-form-item class="btn-box" style="text-align: right;">
        <router-link class="link-div" :to="{ path: '/wjmm' }">忘记密码</router-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const router = useRouter();
const labelPosition = ref('right');
const remember = ref(false);
const formLabelAlign = reactive({
  username: '',
  password: '',
});

const rules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
});

const login = async () => {
  try {
    const response = await axios.post('http://192.168.43.173:8080/auth/login', {
      username: formLabelAlign.username,
      password: formLabelAlign.password,
    });

    if (response.data.code === 200) {
      ElMessage.success('登录成功');
      localStorage.setItem('token', response.data.data.token); // 注意返回数据结构
      localStorage.setItem('userType', response.data.data.userType); // 保存用户类型
      
      // 根据数字类型判断
      if (response.data.data.userType === 1) { // 教师
        router.push('/main');
      } else if (response.data.data.userType === 0) { // 学生
        router.push('/home');
      }
      
      // 记住密码功能
      if (remember.value) {
        localStorage.setItem('username', formLabelAlign.username);
        localStorage.setItem('password', formLabelAlign.password);
      } else {
        localStorage.removeItem('username');
        localStorage.removeItem('password');
      }
    } else {
      ElMessage.error(response.data.message || '登录失败');
    }
  } catch (error) {
    ElMessage.error('登录失败，请检查网络或联系管理员');
    console.error('登录错误:', error);
  }
};

const goToRegister = () => {
  router.push('/register');
};

onMounted(() => {
  const savedUsername = localStorage.getItem('username');
  const savedPassword = localStorage.getItem('password');
  if (savedUsername && savedPassword) {
    formLabelAlign.username = savedUsername;
    formLabelAlign.password = savedPassword;
    remember.value = true;
  }
});
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 1510px;
  height: 720px;
  background-image: url('@/assets/image/login.jpg');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-container {
  width: 350px;
  padding: 35px;
  border-radius: 6px;
  box-shadow: 0 0 20px 0 #c7c6c6;
  background-color: rgba(255, 255, 255, 0.8);
}

.link-div {
  color: red;
  text-decoration: none;
  font-family: sans-serif;
  font-size: 10px;
}
</style>